package constant.milk.cashbook.insert;

import java.util.ArrayList;

import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.asset.bank.BankActivity;
import constant.milk.cashbook.asset.bank.BankInsertActivity;
import constant.milk.cashbook.asset.bank.BankUpdateActivity;
import constant.milk.cashbook.asset.check.CheckActivity;
import constant.milk.cashbook.asset.check.CheckInsertActivity;
import constant.milk.cashbook.asset.check.CheckUpdateActivity;
import constant.milk.cashbook.asset.credit.CreditActivity;
import constant.milk.cashbook.asset.credit.CreditInsertActivity;
import constant.milk.cashbook.asset.credit.CreditUpdateActivity;
import constant.milk.cashbook.asset.gift.GiftActivity;
import constant.milk.cashbook.asset.gift.GiftInsertActivity;
import constant.milk.cashbook.asset.gift.GiftUpdateActivity;
import constant.milk.cashbook.asset.phone.PhoneActivity;
import constant.milk.cashbook.asset.phone.PhoneInsertActivity;
import constant.milk.cashbook.asset.phone.PhoneUpdateActivity;
import constant.milk.cashbook.asset.real.RealActivity;
import constant.milk.cashbook.asset.real.RealInsertActivity;
import constant.milk.cashbook.asset.real.RealUpdateActivity;
import constant.milk.cashbook.bean.BankData;
import constant.milk.cashbook.bean.CheckData;
import constant.milk.cashbook.bean.CreditData;
import constant.milk.cashbook.bean.GiftData;
import constant.milk.cashbook.bean.PhoneData;
import constant.milk.cashbook.bean.RealData;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.util.MilkTextView;
import constant.milk.cashbook.util.MilkUtil;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class AssetListActivity extends SuperActivity
{
	public static final String ASSET_NAME_DATA = "ASSET_NAME_DATA";
	public static final String ASSET_TYPE_DATA = "ASSET_TYPE_DATA";
	
	private static final int INSERT_REQUEST_CODE = 0;
	private static final int UPDATE_REQUEST_CODE = 1;
	
	private ListView listView;
	
	private Button checkButton;
	private Button upDeleteButton;
	private Button addButton; 
	
	private AssetListAdapter listAdapter;
	
	private String items;
	
	private String asset;
	
	private String id;
	
	private DBSelectQuery select;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.asset_list_activity);
		
		setTitle("자산목록");
		
		setGoneTipButton();
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		listView = (ListView) findViewById(R.id.listView);
		
		checkButton = (Button) findViewById(R.id.checkButton);
		upDeleteButton = (Button) findViewById(R.id.upDeleteButton);
		addButton = (Button) findViewById(R.id.addButton);
	}

	@Override
	protected void setOnClick()
	{
		checkButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (items.equals(""))
				{
					MilkUtil.showDialog(superContext, "알림", "자산이 하나도 없습니다. 자산을 추가해주세요.");
					return;
				}
				
				Intent intent = new Intent();
				intent.putExtra(ASSET_NAME_DATA, items);
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
		
		upDeleteButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (id.equals(""))
				{
					MilkUtil.showDialog(superContext, "알림", "선택된 자산 하나도 없습니다.");
					return;
				}
				
				startAssetUpdate();
			}
		});
		
		addButton.setOnClickListener(new  OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				startAssetInsert();
			}
		});
	}

	@Override
	protected void setData()
	{
		select = new DBSelectQuery(superContext, superDb);
		
		id = "";
		
		Intent intent = getIntent();
		items = intent.getStringExtra(ASSET_NAME_DATA);
		asset = intent.getStringExtra(ASSET_TYPE_DATA);
		
		ArrayList<AssetListData> list = getList();
		
		listView.setOnItemClickListener(onItemClickListener);
		
		listAdapter = new AssetListAdapter(
				superContext, R.layout.radio_list_item, list);
		listView.setAdapter(listAdapter);
	}
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3)
		{
			AssetListData data = (AssetListData) arg0.getItemAtPosition(position);
			id = data.getId();
			items = data.getName();
			listAdapter.notifyDataSetChanged();
		}
	};
	
	@Override
	public void onBackPressed()
	{
		Intent intent = new Intent();
		intent.putExtra(ASSET_NAME_DATA, items);
		setResult(Activity.RESULT_OK, intent);
		
		super.onBackPressed();
	}
	
	private void startAssetInsert()
	{
		if (asset.equals(getString(R.string.realAsset)))
		{
			Intent intent = new Intent(superContext, RealInsertActivity.class);
			startActivityForResult(intent, INSERT_REQUEST_CODE);
		}
		else if (asset.equals(getString(R.string.bankAsset)))
		{
			Intent intent = new Intent(superContext, BankInsertActivity.class);
			startActivityForResult(intent, INSERT_REQUEST_CODE);
		}
		else if (asset.equals(getString(R.string.checkCardAsset)))
		{
			Intent intent = new Intent(superContext, CheckInsertActivity.class);
			startActivityForResult(intent, INSERT_REQUEST_CODE);
		}
		else if (asset.equals(getString(R.string.creditCardAsset)))
		{
			Intent intent = new Intent(superContext, CreditInsertActivity.class);
			startActivityForResult(intent, INSERT_REQUEST_CODE);
		}
		else if (asset.equals(getString(R.string.phoneAsset)))
		{
			Intent intent = new Intent(superContext, PhoneInsertActivity.class);
			startActivityForResult(intent, INSERT_REQUEST_CODE);
		}
		else if (asset.equals(getString(R.string.giftCardAsset)))
		{
			Intent intent = new Intent(superContext, GiftInsertActivity.class);
			startActivityForResult(intent, INSERT_REQUEST_CODE);
		}
	}
	
	private void startAssetUpdate()
	{
		if (asset.equals(getString(R.string.realAsset)))
		{
			Intent intent = new Intent(superContext, RealUpdateActivity.class);
			intent.putExtra(RealActivity.REAL_ID, id);
			startActivityForResult(intent, UPDATE_REQUEST_CODE);
		}
		else if (asset.equals(getString(R.string.bankAsset)))
		{
			Intent intent = new Intent(superContext, BankUpdateActivity.class);
			intent.putExtra(BankActivity.BANK_ID, id);
			startActivityForResult(intent, UPDATE_REQUEST_CODE);
		}
		else if (asset.equals(getString(R.string.checkCardAsset)))
		{
			Intent intent = new Intent(superContext, CheckUpdateActivity.class);
			intent.putExtra(CheckActivity.CHECK_ID, id);
			startActivityForResult(intent, UPDATE_REQUEST_CODE);
		}
		else if (asset.equals(getString(R.string.creditCardAsset)))
		{
			Intent intent = new Intent(superContext, CreditUpdateActivity.class);
			intent.putExtra(CreditActivity.CREDIT_ID, id);
			startActivityForResult(intent, UPDATE_REQUEST_CODE);
		}
		else if (asset.equals(getString(R.string.phoneAsset)))
		{
			Intent intent = new Intent(superContext, PhoneUpdateActivity.class);
			intent.putExtra(PhoneActivity.PHONE_ID, id);
			startActivityForResult(intent, UPDATE_REQUEST_CODE);
		}
		else if (asset.equals(getString(R.string.giftCardAsset)))
		{
			Intent intent = new Intent(superContext, GiftUpdateActivity.class);
			intent.putExtra(GiftActivity.BANK_ID, id);
			startActivityForResult(intent, UPDATE_REQUEST_CODE);
		}
	}
	
	private ArrayList<AssetListData> getList()
	{
		ArrayList<AssetListData> assetList = new ArrayList<AssetListData>();
		
		if (asset.equals(getString(R.string.realAsset)))
		{
			ArrayList<RealData> list = select.getRealList();
			
			for (int i = 0; i < list.size(); i++)
			{
				AssetListData data = new AssetListData();
				data.setId(list.get(i).getId());
				data.setName(list.get(i).getName());
				assetList.add(data);
			}
		}
		else if (asset.equals(getString(R.string.bankAsset)))
		{
			ArrayList<BankData> list = select.getBankList();
			
			for (int i = 0; i < list.size(); i++)
			{
				AssetListData data = new AssetListData();
				data.setId(list.get(i).getId());
				data.setName(list.get(i).getName());
				assetList.add(data);
			}
		}
		else if (asset.equals(getString(R.string.checkCardAsset)))
		{
			ArrayList<CheckData> list = select.getCheckList();
			
			for (int i = 0; i < list.size(); i++)
			{
				AssetListData data = new AssetListData();
				data.setId(list.get(i).getId());
				data.setName(list.get(i).getName());
				assetList.add(data);
			}
		}
		else if (asset.equals(getString(R.string.creditCardAsset)))
		{
			ArrayList<CreditData> list = select.getCreditList();
			
			for (int i = 0; i < list.size(); i++)
			{
				AssetListData data = new AssetListData();
				data.setId(list.get(i).getId());
				data.setName(list.get(i).getName());
				assetList.add(data);
			}
		}
		else if (asset.equals(getString(R.string.phoneAsset)))
		{
			ArrayList<PhoneData> list = select.getPhoneList();
			
			for (int i = 0; i < list.size(); i++)
			{
				AssetListData data = new AssetListData();
				data.setId(list.get(i).getId());
				data.setName(list.get(i).getName());
				assetList.add(data);
			}
		}
		else if (asset.equals(getString(R.string.giftCardAsset)))
		{
			ArrayList<GiftData> list = select.getGiftList();
			
			for (int i = 0; i < list.size(); i++)
			{
				AssetListData data = new AssetListData();
				data.setId(list.get(i).getId());
				data.setName(list.get(i).getName());
				assetList.add(data);
			}
		}
		
		return assetList;
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == INSERT_REQUEST_CODE)
		{
			ArrayList<AssetListData> list = getList();
			
			if (items.equals("") && list.size() > 0)
			{
				id = list.get(0).getId();
				items = list.get(0).getName();
			}
			
			listView.setOnItemClickListener(onItemClickListener);
			
			listAdapter = new AssetListAdapter(
					superContext, R.layout.radio_list_item, list);
			listView.setAdapter(listAdapter);
		}
		else if (requestCode == UPDATE_REQUEST_CODE)
		{
			ArrayList<AssetListData> list = getList();
			
			if (list.size() > 0)
			{
				for (int i = 0; i < list.size(); i++)
				{
					if (id.equals(list.get(i).getId()))
					{
						items = list.get(i).getName();
						break;
					}
					else
					{
						items = list.get(0).getName();
					}
				}
			}
			else
			{
				id = "";
				items = "";
			}
			
			listView.setOnItemClickListener(onItemClickListener);
			
			listAdapter = new AssetListAdapter(
					superContext, R.layout.radio_list_item, list);
			listView.setAdapter(listAdapter);
		}
	}
	
	/**
	 * 리스트 아답터
	 */
	private class AssetListAdapter extends ArrayAdapter<AssetListData> 
	{
		public AssetListAdapter(Context context, int textViewResourceId, ArrayList<AssetListData> list) 
		{
			super(context, textViewResourceId, list);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.radio_list_item, null);
			}
			
			AssetListData data = getItem(position);
			
			MilkTextView nameTextView = (MilkTextView) v.findViewById(R.id.radioItemNameTextView);
			RadioButton radioButton = (RadioButton) v.findViewById(R.id.radioItemRadio);
			
			nameTextView.setTextSize(TEXT_SIZE);
			nameTextView.setText(data.getName());
			
			if (items.equals(data.getName()))
			{
				radioButton.setChecked(true);
				id = data.getId();
			}
			else
			{
				radioButton.setChecked(false);
			}
			
			return v;
		}
	}
	
	private class AssetListData
	{
		private String id;
		private String name;
		
		public String getId()
		{
			return id;
		}
		public void setId(String id)
		{
			this.id = id;
		}
		public String getName()
		{
			return name;
		}
		public void setName(String name)
		{
			this.name = name;
		}
	}

	@Override
	protected void tipButton()
	{
	}

}
